package com.mj.mafeed.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.mj.dollars.AppConnect;
import com.mj.mafeed.R;
import com.mj.mafeed.adapter.ForumTopicAdapter;
import com.mj.mafeed.bean.ForumTopicJSON;
import com.mj.mafeed.bean.ForumTopicListJSON;
import com.mj.mafeed.service.ForumTopicService;
import com.mj.mafeed.swipeback.SwipeBackActivity;
import com.mj.mafeed.util.DateUtils;
import com.mj.mafeed.util.NetUtils;
import com.mj.mafeed.util.SPUtils;
import com.mj.mafeed.widget.LoadingDialog;
import com.mj.mafeed.widget.XListView;
import com.mj.mafeed.widget.XListView.IXListViewListener;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * 资讯
 * @author zhaominglei
 * @date 2016-1-12
 * 
 */
public class ForumTopicActivity extends SwipeBackActivity implements OnClickListener,IXListViewListener {
	@SuppressWarnings("unused")
	private static final String TAG = ForumTopicActivity.class.getSimpleName();
	private ForumTopicService forumTopicService = new ForumTopicService();
	private boolean isExit = false;
	private TimerTask timerTask;
	private ImageView forumtopicAdd;
	@SuppressWarnings("unused")
	private ImageView forumtopicMessage;
	private LoadingDialog loadingDialog; //加载框
	public XListView forumTopicsView;
	private List<ForumTopicJSON> forumTopics = new ArrayList<ForumTopicJSON>();
	public ForumTopicAdapter forumTopicAdapter;
	private int page = 1; //页码
	@SuppressWarnings("unused")
	private String referer; // 来自那个链接
	private String username; //用户名
	private String sign; //签名
	private Intent intent;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_forumtopic);
		init();
	}
	
	private void init() {
		username = (String)SPUtils.get(getApplicationContext(), "username", "");
		sign = (String)SPUtils.get(getApplicationContext(), "sign", "");
		forumtopicAdd = (ImageView)findViewById(R.id.forumtopic_add);
		forumtopicMessage = (ImageView)findViewById(R.id.forumtopic_message);
		forumTopicsView = (XListView)findViewById(R.id.forumtopic_listview);
		forumTopicAdapter = new ForumTopicAdapter(ForumTopicActivity.this, forumTopics, username, sign);
		forumTopicsView.setPullLoadEnable(true);
		forumTopicsView.setXListViewListener(this);
		forumTopicsView.setAdapter(forumTopicAdapter);

		forumtopicAdd.setOnClickListener(this);
		loadingDialog = new LoadingDialog(this);
		getForumTopics();
	}

	private void getForumTopics() {
		if (NetUtils.isConnected(getApplicationContext())) {
			loadingDialog.show();
			new ForumTopicsAsyncTask().execute("");
		} else {
			Toast.makeText(getApplicationContext(), R.string.net_error, Toast.LENGTH_SHORT).show();
			onLoad();
		}
	}

	@Override
	protected void onResume() {
		getForumTopics();
		super.onResume();
	}

	@Override
	public void onRefresh() {
		getForumTopics();
	}

	@Override
	public void onLoadMore() {
		getForumTopics();
		page++;
	}
	
	//刷新结束
	public void onLoad() {
		if (forumTopicsView != null) {
			forumTopicsView.stopRefresh();
			forumTopicsView.stopLoadMore();
			forumTopicsView.setRefreshTime(DateUtils.format(new Date(), DateUtils.datePattern));
		}
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.forumtopic_add:
			if (username != null && !username.equals("") && sign != null && !sign.equals("")) {
				intent  = new Intent(ForumTopicActivity.this, ForumTopicAddActivity.class);
				intent.putExtra("referer", "ForumTopicActivity");
				startActivity(intent);
			} else {
				intent = new Intent(ForumTopicActivity.this, LoginActivity.class);
				intent.putExtra("referer", "ForumTopicActivity");
				startActivity(intent);
			}
			break;
		case R.id.forumtopic_message:
			
			break;
			
		default:
			break;
		}
	}

	@Override
	public void onBackPressed() {
		if (isExit) {
			AppConnect.getInstance(this).close();
			ForumTopicActivity.this.finish();
		} else {
			isExit = true;
			Toast.makeText(ForumTopicActivity.this, R.string.exit_msg, Toast.LENGTH_SHORT).show();
			timerTask = new TimerTask() {
				@Override
				public void run() {
					isExit = false;
				}
			};
			new Timer().schedule(timerTask, 2*1000);
		}
	}
	
	public class ForumTopicsAsyncTask extends AsyncTask<String, String, ForumTopicListJSON> {
		@Override
		protected ForumTopicListJSON doInBackground(String... params) {
			return forumTopicService.getForumTopic("ForumTopicActivity", ForumTopicJSON.NEWS_TOPIC_TYPE, page);
		}

		@Override
		protected void onPostExecute(ForumTopicListJSON result) {
			loadingDialog.dismiss();
			if (result != null && result.getRecordList() != null && result.getRecordList().size() > 0) {
				forumTopicAdapter.setForumTopics(result.getRecordList());
				forumTopicAdapter.notifyDataSetChanged();
				onLoad();
			}
			if (result != null && !result.isHasNextPage()) {
				Toast.makeText(getApplicationContext(), R.string.load_more_finish,	Toast.LENGTH_SHORT).show();
				forumTopicsView.setPullLoadEnable(false);
			} 
		}
	}
}
